package com.qdlc.p2p.biz.processor.event;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import com.qdlc.p2p.biz.processor.task.AbstractNoticeTemplateTaskProcessor;
import com.qdlc.p2p.biz.processor.task.NoticeTemplateEmailTaskProcessor;
import com.qdlc.p2p.biz.processor.task.NoticeTemplateMessageTaskProcessor;
import com.qdlc.p2p.biz.processor.task.NoticeTemplateSmsTaskProcessor;
import com.qdlc.p2p.common.core.queue.event.AbstractReplayValueEvent;
import com.qdlc.p2p.dal.common.NoticeTypeHelper;
import com.qdlc.p2p.dal.common.NoticeTypeHelper.NidType;
import com.qdlc.p2p.dal.dto.NoticeType;
import com.qdlc.p2p.dal.dto.User;

/**
 * 短信、邮件、站内信队列事件，通过事件发起通知
 * @author linya 2016-01-17
 */
public class NoticeTemplateValueEvent extends AbstractReplayValueEvent implements Serializable {

	private static final long serialVersionUID = 4687692511994370751L;

	private User user;
	
	private String noticeTypeNid;
	
	private NidType nidType;	//需要指明发送类型
	
	private final Map<String, Object> sendData = new HashMap<String, Object>();
	
	//短信、邮件、站内信发送任务处理器
	private static final Map<NidType, Class<? extends AbstractNoticeTemplateTaskProcessor>> noticeTemplateTaskProcessorMaps = new HashMap<NoticeTypeHelper.NidType, Class<? extends AbstractNoticeTemplateTaskProcessor>>();

	static{
		noticeTemplateTaskProcessorMaps.put(NidType.NOTICE_SMS, NoticeTemplateSmsTaskProcessor.class);
		noticeTemplateTaskProcessorMaps.put(NidType.NOTICE_EMAIL, NoticeTemplateEmailTaskProcessor.class);
		noticeTemplateTaskProcessorMaps.put(NidType.NOTICE_MESSAGE, NoticeTemplateMessageTaskProcessor.class);
	}
	
	private NoticeTemplateValueEvent(){}
	
	private void initTaskProcessorClass(){
		if(nidType == null){
			throw new IllegalArgumentException("the 'nidType' field of NoticeTemplateValueEvent is null!");
		}
		NoticeType noticeType = NoticeTypeHelper.getValue(noticeTypeNid, nidType);
		if(noticeType != null){
			Class<? extends AbstractNoticeTemplateTaskProcessor> taskProcessorClass = noticeTemplateTaskProcessorMaps.get(nidType);
			this.setTaskProcessorClass(taskProcessorClass);
		}
	}
	
	public static NoticeTemplateValueEvent newInstance(User user, String noticeTypeNid, NidType nidType){
		NoticeTemplateValueEvent event = new NoticeTemplateValueEvent();
		event.user = user;
		event.noticeTypeNid = noticeTypeNid;
		event.nidType = nidType;
		event.initTaskProcessorClass();
		return event;
	}

	public User getUser() {
		return user;
	}

	public String getNoticeTypeNid() {
		return noticeTypeNid;
	}

	public Map<String, Object> getSendData() {
		return sendData;
	}

	public NoticeTemplateValueEvent addSendData(Map<String, Object> sendData) {
		this.sendData.putAll(sendData);
		return this;
	}
	
	public NoticeTemplateValueEvent addSendData(String key, Object value) {
		this.sendData.put(key, value);
		return this;
	}
	
}
